package HttpPages;

import java.util.logging.Level;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

import Database.Database;
import Logger.AppLogger;
import Objects.UserProfile;

@Path("/updatePosition")
public class updatePosition 
{
	//-----------------------------------http methods--------------------------------

	// This method is called if TEXT_PLAIN is request
	@POST
	@Consumes(MediaType.TEXT_PLAIN)
	public String openConnectionRequest(@FormParam("my_android_id") String my_android_id,
			@FormParam("latitude") String latitude, @FormParam("longitude") String longitude)
	{
		AppLogger.write(Level.INFO, my_android_id +" Requested updatePosition.");

		//finding in the database the corresponding C2DM registration_id of the destination of this message
		UserProfile userProf = Database.getInstance().getUserProfile(my_android_id);
		
		if(userProf == null)
		{
			AppLogger.write(Level.WARNING, "Can't updatePosition to an Unknown user "+ my_android_id);
			return "your android_id ( "+ my_android_id +" ) was not found!";
			//"your user profile was not found! "+my_android_id;
		}
		
		userProf.updatePosition(Double.valueOf(latitude), Double.valueOf(longitude));
		System.out.println("updated: " + userProf.getX()+"," + userProf.getY());
		
		AppLogger.write(Level.INFO, my_android_id + " changed position. ( " + latitude + " , " + longitude + " )");
		
		//TODO  -  need to create Response codes file and handle everything!!!
		return "your position was updated!";
	}


}
